﻿Imports System.IO
Imports System.Windows.Forms

Public Class InventoryItem
    Private _id As Integer = -1
    Private _name As String = ""
    Private _desc As String = ""
    Private _itemTypeID As Integer = -1
    Private _iconImage As String = ""
    Private _descImage As String = ""
    Private _reqSkillID As Integer = -1
    Private _healDice As New diceRoll
    Private _damageDice As New diceRoll
    Private _defenceValue As Integer
    Private _keyNum As Integer
    Private _chargeItemID As Integer
    Private _numCharges As Integer

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property

    Public Property ItemTypeID() As Integer
        Get
            Return _itemTypeID
        End Get
        Set(ByVal value As Integer)
            _itemTypeID = value
        End Set
    End Property

    Public Property IconImageName() As String
        Get
            Return _iconImage
        End Get
        Set(ByVal value As String)
            _iconImage = value
        End Set
    End Property

    Public Property DescriptiveImageName() As String
        Get
            Return _descImage
        End Get
        Set(ByVal value As String)
            _descImage = value
        End Set
    End Property

    Public Property RequiredSkillID() As Integer
        Get
            Return _reqSkillID
        End Get
        Set(ByVal value As Integer)
            _reqSkillID = value
        End Set
    End Property

    Public Property HealDice() As diceRoll
        Get
            Return _healDice
        End Get
        Set(ByVal value As diceRoll)
            _healDice = value
        End Set
    End Property

    Public Property DamageDice() As diceRoll
        Get
            Return _damageDice
        End Get
        Set(ByVal value As diceRoll)
            _damageDice = value
        End Set
    End Property

    Public Property DefenceValue() As Integer
        Get
            Return _defenceValue
        End Get
        Set(ByVal value As Integer)
            _defenceValue = value
        End Set
    End Property

    Public Property KeyNumber() As Integer
        Get
            Return _keyNum
        End Get
        Set(ByVal value As Integer)
            _keyNum = value
        End Set
    End Property

    Public Property ChargesItemID() As Integer
        Get
            Return _chargeItemID
        End Get
        Set(ByVal value As Integer)
            _chargeItemID = value
        End Set
    End Property

    Public Property NumberOfCharges() As Integer
        Get
            Return _numCharges
        End Get
        Set(ByVal value As Integer)
            _numCharges = value
        End Set
    End Property

    Public Function ToXElement() As XElement
        Dim itemXML = <item id=<%= _id %>>
                          <name><%= _name %></name>
                          <desc><%= _desc %></desc>
                          <type><%= _itemTypeID.ToString %></type>
                          <iconImage><%= _iconImage %></iconImage>
                          <descImage><%= _descImage %></descImage>
                          <skill><%= _reqSkillID %></skill>
                          <healDice><%= _healDice.ToString %></healDice>
                          <damageDice><%= _damageDice.ToString %></damageDice>
                          <defence><%= _defenceValue.ToString %></defence>
                          <key><%= _keyNum.ToString %></key>
                          <chargeItem><%= _chargeItemID.ToString %></chargeItem>
                          <charges><%= _numCharges.ToString %></charges>
                      </item>
        Return itemXML
    End Function

    Public Sub LoadXML(ByVal itemXMLString As String)
        Dim sr As New StringReader(itemXMLString)
        Dim itemX As XElement

        Try
            itemX = XElement.Load(sr)
        Catch ex As Exception
            Failed(itemXMLString, ex.ToString)
            Exit Sub
        End Try

        Try
            _id = Integer.Parse(itemX.@id)
            _name = itemX.<name>.Value
            _desc = itemX.<desc>.Value
            If Integer.TryParse(itemX.<type>.Value, _itemTypeID) = False Then
                Failed(itemXMLString, "ItemTypeID invalid integer")
                Exit Sub
            End If
            _iconImage = itemX.<iconImage>.Value
            _descImage = itemX.<descImage>.Value
            If Integer.TryParse(itemX.<skill>.Value, _reqSkillID) = False Then
                Failed(itemXMLString, "RequredSkillID invalid integer")
                Exit Sub
            End If
            _healDice.FromString = itemX.<healDice>.Value
            _damageDice.FromString = itemX.<damageDice>.Value
            If Integer.TryParse(itemX.<defence>.Value, _defenceValue) = False Then
                Failed(itemXMLString, "DefenceValue invalid integer")
                Exit Sub
            End If
            If Integer.TryParse(itemX.<key>.Value, _keyNum) = False Then
                Failed(itemXMLString, "KeyNumber invalid integer")
                Exit Sub
            End If
            If Integer.TryParse(itemX.<chargeItem>.Value, _chargeItemID) = False Then
                Failed(itemXMLString, "ChargesItemID invalid integer")
                Exit Sub
            End If
            If Integer.TryParse(itemX.<charges>.Value, _numCharges) = False Then
                Failed(itemXMLString, "NumberOfCharges invalid integer")
                Exit Sub
            End If
        Catch ex As Exception
            Failed(itemXMLString, ex.ToString)
            Exit Sub
        End Try

    End Sub

    Private Sub Failed(ByVal xmlString As String, ByVal ex As String)
        MessageBox.Show(xmlString + vbCrLf + vbCrLf + ex, "Failed to LoadXML", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Public Function IsValid() As Boolean
        If _name = "" Then
            Return False
        ElseIf _desc = "" Then
            Return False
        ElseIf _itemTypeID = -1 Then
            Return False
        ElseIf _iconImage = "" Then
            Return False
        ElseIf _descImage = "" Then
            Return False
        ElseIf _reqSkillID = -1 Then
            Return False
        End If

        Return True
    End Function
End Class
